/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.prashant.sqlduniya.editor.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "File",
id = "org.prashant.sqlduniya.editor.action.OpenSQLAction")
@ActionRegistration(iconBase = "org/prashant/sqlduniya/editor/action/open-folder-icon16.png",
displayName = "#CTL_OpenSQLAction")
@ActionReferences({
    @ActionReference(path = "Menu/File", position = 1200),
    @ActionReference(path = "Shortcuts", name = "D-O")
})
@Messages("CTL_OpenSQLAction=Open")
public final class OpenSQLAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new FileNameExtensionFilter(
                "SQL Script (.sql)", "sql"));

        int returnVal = fileChooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
//            try {
                // What to do with the file, e.g. display it in a TextArea
                //textarea.read(new FileReader(file.getAbsolutePath()), null);
                System.out.println("File Open " + file.getAbsolutePath());
//            } catch (IOException ex) {
//                System.out.println("problem accessing file" + file.getAbsolutePath());
//            }
        } else {
            System.out.println("File access cancelled by user.");
        }

    }
}
